package com.jkdz.spyke.widgetUIs.fileSharer {
	import spark.components.Group;
	import spark.components.Label;
	import spark.effects.Fade;
	import spark.layouts.HorizontalAlign;
	import spark.layouts.HorizontalLayout;
	import spark.layouts.VerticalAlign;

	import com.jkdz.spyke.flexUI.helpers.Link;
	import com.jkdz.spyke.flexUI.main.Main;
	import com.jkdz.spyke.rs.FileInfo;
	import com.jkdz.spyke.widgetUIs.SpykeFileSharerUI;

	import mx.binding.utils.BindingUtils;
	import mx.containers.Canvas;

	import flash.events.MouseEvent;
	import flash.net.URLRequest;
	import flash.net.navigateToURL;
	
	public class SpykeOfferedFileIcon extends Group {
		
		private var fileID:String;
		private var bg:Canvas;
		private var owned:Boolean;
		private var link:Link;
		private var widgetUI:SpykeFileSharerUI;
		private var fileName:String;
		private var act:Canvas;
		
		public function codeInit(info:FileInfo, requestedWidth:int, widgetUI:SpykeFileSharerUI):void {
			this.fileID = info.id;
			this.fileName = info.name;
			this.widgetUI = widgetUI;
			this.width = requestedWidth;
			this.height = 30;
			
			bg = new Canvas();
			bg.width = this.width;
			bg.height = this.height;

			var content:Group = new Group();
			var hl:HorizontalLayout = new HorizontalLayout();
			hl.paddingBottom = hl.paddingLeft = hl.paddingRight = hl.paddingTop = 5;
			hl.gap = 10;
			hl.verticalAlign = VerticalAlign.MIDDLE;
			hl.horizontalAlign = HorizontalAlign.LEFT;
			content.layout = hl;
			content.width = this.width;
			content.height = this.height;
			
			var additional:Group = new Group();
			var al:HorizontalLayout = new HorizontalLayout();
			al.paddingBottom = al.paddingLeft = al.paddingRight = al.paddingTop = 5;
			al.gap = 10;
			al.verticalAlign = VerticalAlign.MIDDLE;
			al.horizontalAlign = HorizontalAlign.RIGHT;
			additional.layout = al;
			additional.width = this.width;
			additional.height = this.height;
			
			var size:Label = new Label();
			size.setStyle("color", 0xffdddddd);
			size.text = formatSize(info.length);
			additional.addElement(size);
			
			link = new Link(info.name, true);
			link.addEventListener(MouseEvent.CLICK, onClick);
			content.addElement(link);
			
			act = new Canvas();
			act.visible = false;
			act.width = act.height = 8;
			act.graphics.lineStyle(2, 0xfffffffe, 0.2);
			act.graphics.beginFill(0xfffffffe, 0.4);
			act.graphics.drawCircle(act.width/2, act.height/2, act.width/2);
			act.graphics.endFill();
			act.x = requestedWidth-75;
			act.y = (this.height-act.width)/2;
			
			this.addElement(bg);
			this.addElement(content);
			this.addElement(act);
			this.addElement(additional);
			
			BindingUtils.bindSetter(function(n:Number):void {
				if (n > requestedWidth-85) {
					link.text = link.text.substring(0, link.text.length-4)+"...";
				}
			}, link, "width");

			setOwned(false);
			
			this.alpha = 0.0;
			
			var inEffect:Fade = new Fade(this);
			inEffect.duration = 300;
			inEffect.alphaFrom = 0.0;
			inEffect.alphaTo = 1.0;
			this.setStyle("addedEffect", inEffect);
			
			this.addEventListener(MouseEvent.ROLL_OVER, function(e:MouseEvent):void {
				bg.alpha = 0.8;
			});
			
			this.addEventListener(MouseEvent.ROLL_OUT, function(e:MouseEvent):void {
				bg.alpha = 1.0;
			});
		}

		private function onClick(event:MouseEvent):void {
			if (owned) {
				widgetUI.onDelete(fileID);
			} else {
				navigateToURL(new URLRequest(Main.WG_LOCATION+"/FileSharer/"+fileID));
			}
		}

		private function formatSize(length:Number):String {
			if (length < 1024) {
				return length + " B";
			}
			length = length/1024.0;
			if (length < 1024) {
				return ((int)(length*10))/10.0 + " kB";
			}
			length = length/1024.0;
			if (length < 1024) {
				return ((int)(length*10))/10.0 + " MB";
			}
			length = length/1024.0;
			return ((int)(length*10))/10.0 + " GB";
		}

		public function setActivity(act:Boolean):void {
			this.act.visible = act;
		}
		
		public function setOwned(owned:Boolean):void {
			this.owned = owned;
			link.toolTip = owned ? ("Remove my "+fileName) : ("Download "+fileName);
			bg.graphics.clear();
			bg.graphics.lineStyle(1, 0xff000000, 1.0);
			bg.graphics.beginFill(owned ? 0xff880000 : 0xff003300, 0.5);
			bg.graphics.drawRect(0, 0, bg.width, bg.height);
			bg.graphics.endFill();
		}
	}
}